package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.dto.ReciteRecordDo;
import com.example.demo.entity.ReciteNum;
import com.example.demo.entity.ReciteRecord;
import com.example.demo.entity.ReciteTime;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


@Mapper
public interface ReciteRecordMapper extends BaseMapper<ReciteRecord> {

    @Select("select ifNull(sum(spend_time),0) as spendTime,ifNull(sum(recite_count),0) as reciteCount from recite_record where user_id = #{userId} " +
            "and to_days(now())-to_days(create_time) = 0")
    ReciteRecordDo selectTodayCount(@Param("userId") Long userId);

    @Select("select ifNull(sum(spend_time),0) as spendTime,ifNull(sum(recite_count),0) as reciteCount from recite_record where user_id = #{userId} " +
            "and to_days(now())-to_days(create_time) <= #{dayCount}")
    ReciteRecordDo selectByUserId(Long userId, Integer dayCount);

    @Select("SELECT a.user_id AS userId,SUM(a.spend_time) AS spendTime,b.`username` as username FROM recite_record AS a LEFT JOIN USER AS b ON \n" +
            " a.`user_id` = b.`id` GROUP BY user_id")
    List<ReciteTime> selectReciteTime();

    @Select("SELECT a.user_id AS userId,SUM(a.recite_count) AS reciteNum,b.`username` as username FROM recite_record AS a LEFT JOIN USER AS b ON \n" +
            " a.`user_id` = b.`id` GROUP BY user_id")
    List<ReciteNum> selectReciteNum();
}
